package com.vipheyue.huaxue.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.vipheyue.huaxue.R;
import com.vipheyue.huaxue.bean.MessageBean;
import com.vipheyue.huaxue.bean.MyUser;
import com.vipheyue.huaxue.bean.ThingBean;
import com.vipheyue.huaxue.utils.GsonUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

public class DetailActivity extends AppCompatActivity {

    @Bind(R.id.iv_Menu_leftImg)
    ImageView iv_Menu_leftImg;
    @Bind(R.id.tv_Menu_leftTitle)
    TextView tv_Menu_leftTitle;
    @Bind(R.id.rl_menu_left)
    RelativeLayout rl_menu_left;
    @Bind(R.id.tv_Menu_Title)
    TextView tv_Menu_Title;
    @Bind(R.id.iv_menu_rightTitle)
    TextView iv_menu_rightTitle;
    @Bind(R.id.iv_menu_right)
    ImageView iv_menu_right;
    @Bind(R.id.rl_menu_right)
    RelativeLayout rl_menu_right;
    @Bind(R.id.et_detail_name)
    EditText et_detail_name;
    @Bind(R.id.et_detail_weight)
    EditText et_detail_weight;
    @Bind(R.id.et_detail_purity)
    EditText et_detail_purity;
    @Bind(R.id.et_detail_Deliverydate)
    EditText et_detail_Deliverydate;
    @Bind(R.id.et_detail_structure)
    EditText et_detail_structure;
    @Bind(R.id.btn_detail_commit)
    Button btn_detail_commit;
    @Bind(R.id.btn_detail_browse)
    Button btn_detail_browse;
    private ThingBean thingBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        tv_Menu_Title.setText("求购");
        Intent intent = this.getIntent();
        String jobStr = intent.getStringExtra("jobStr");
        thingBean = GsonUtils.fromJson(jobStr, ThingBean.class);
        MyUser user = BmobUser.getCurrentUser(this, MyUser.class);
        if (user == null) {
            startActivity(new Intent(this,LoginActivity.class));
            return;
        }
        if (thingBean.getReleaseUser().getObjectId().equals(user.getObjectId())) {//TODO 这里需要判断
//            btn_detail_browse.setVisibility(View.VISIBLE);
            btn_detail_commit.setVisibility(View.INVISIBLE);
        }
        et_detail_name.setText(thingBean.getName());
        et_detail_Deliverydate.setText(thingBean.getDeliverydate());
        et_detail_purity.setText(thingBean.getPurity());
        et_detail_structure.setText(thingBean.getStructure());
        et_detail_weight.setText(thingBean.getWeight());
    }

    @OnClick({R.id.rl_menu_left, R.id.btn_detail_commit,R.id.btn_detail_browse})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_menu_left:
                finish();
                break;
            case R.id.btn_detail_commit:
//                startActivity(new Intent(this, ReleaseActivity.class));
//                finish();
//                Intent intent = new Intent();
//                intent.setAction("android.intent.action.VIEW");
//                Uri content_url = Uri.parse("http://wpa.qq.com/msgrd?v=3&uin=574201314&site=oicqzone.com&menu=yes");
//                intent.setData(content_url);
//                startActivity(intent);
                createAlertDialog().show();
                break;
            case R.id.btn_detail_browse:
                Intent intent=new Intent(this, BrowseMessageActivity.class);
                intent.putExtra("thingBeanId", thingBean.getObjectId());
                startActivity(intent);
                break;
        }
    }

    private AlertDialog createAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_queryhistory, null);
        final EditText editText = (EditText) view.findViewById(R.id.et_history);
        builder.setView(view).setPositiveButton("报价", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (!TextUtils.isEmpty(editText.getText().toString().trim())) {
                    saveInfoToMessageCenter(editText.getText().toString().trim());
                }
            }
        });
        return builder.create();
    }

    private void saveInfoToMessageCenter(String trim) {
        MessageBean bean = new MessageBean();
        bean.setSendTo(thingBean.getReleaseUser());
        bean.setSendBy(BmobUser.getCurrentUser(this, MyUser.class));
        bean.setThingbean(thingBean);
        bean.setContent("报价:" + trim + "元");
        bean.save(this, new SaveListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(DetailActivity.this, "报价成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(DetailActivity.this, "报价失败", Toast.LENGTH_SHORT).show();
            }
        });
    }


}
